package cn.xshi.sys.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.xshi.sys.form.SysNotifyForm;
import cn.xshi.sys.model.SysNotify;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.model.SysNotifyReceiver;
import cn.xshi.sys.service.SysNotifyReceiverService;
import cn.xshi.sys.service.SysNotifyService;
import cn.xshi.sys.service.SysUserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
/**
 * @Desc 通知
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysNotify")
@Api(value = "通知API",tags = "通知API",description = "通知API")
public class SysNotifyController extends BaseAction {

	@Resource
	private SysNotifyService sysNotifyService;

	@Resource
	private SysUserInfoService sysUserinfoService;

	@Resource
	private SysNotifyReceiverService sysNotifyReceiverService;

	/**
	 * 查询通知列表并分页
	 * @param sysNotifyForm
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value="/list")
	@Auth(value = "/sysNotify/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询通知列表并分页", notes="查询通知列表并分页")
	public BasePage<List<SysNotify>> getSysNotifyListByCondition(SysNotifyForm sysNotifyForm,Integer pageNo, Integer pageSize){
		sysNotifyForm.setCreateId(getCurrentAccountId());
		commonHPagerByPageNo(pageNo,pageSize);
		List<SysNotify> sysNotifyList = sysNotifyService.getSysNotifyListByCondition(sysNotifyForm);
		for(SysNotify sysNotify : sysNotifyList){
			if(!StringUtil.isEmpty(sysNotify.getCreateId())){
				OauthAccountEntity createBy = getAccount(sysNotify.getCreateId());
				if(null != createBy){
					sysNotify.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(sysNotify.getCreateId())){
				OauthAccountEntity createBy = getAccount(sysNotify.getCreateId());
				if(null != createBy){
					sysNotify.setCreateBy(createBy.getName());
				}
			}
		}
		PageInfo<SysNotify> page = new PageInfo<SysNotify>(sysNotifyList);
		return outPageBootByPageNo(page,pageNo,pageSize);
	}

	/**
	 * 查询单个通知
	 * @param id
	 */
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysNotify/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询单个通知", notes="查询单个通知")
	public BaseResult<SysNotify> getSysNoticeById(@PathVariable("id")String id){
		SysNotify notify = sysNotifyService.getSysNotifyById(id);
		if(null != notify){
			List<SysNotifyReceiver> notifyReceivers = sysNotifyReceiverService.getSysNotifyReceiverListById(notify.getId());
			notify.setNotifyReceivers(notifyReceivers);
		}
		return BaseResult.success(notify);
	}

	/**
	 * 添加
	 * @param sysNotify
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个通知", notes="创建单个通知")
	public BaseResult addSysNotify(@RequestBody SysNotify sysNotify){
		int i = 0;
		if(null != sysNotify){
			List<SysNotifyReceiver> notifyReceiverList = new ArrayList<SysNotifyReceiver>();
			sysNotify.setId(toUUID());
			sysNotify.setCreateTime(getDate());
			String userId = sysNotify.getCreateId();
			if(!StringUtils.isEmpty(userId)){
				String[] userIds = userId.split(",");
				for(int j = 0; j < userIds.length; j++){
					SysNotifyReceiver sysNotifyReceiver = new SysNotifyReceiver();
					sysNotifyReceiver.setNotifyId(sysNotify.getId());
					sysNotifyReceiver.setId(toUUID());
					sysNotifyReceiver.setReceiveId(userIds[j]);
					sysNotifyReceiver.setReceiveName(sysUserinfoService.getSysUserInfoById(userIds[j]).getName());
					sysNotifyReceiver.setReceiveTime(sysNotify.getCreateTime());
					notifyReceiverList.add(sysNotifyReceiver);
				}
				sysNotify.setNotifyReceivers(notifyReceiverList);
			}
			i= sysNotifyService.addSysNotify(sysNotify);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除单个通知", notes="删除单个通知")
	public BaseResult delSysNotify(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysNotifyService.delSysNotify(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
